<template lang="pug">
.pacs-conclusion-form 
  el-from(v-loading="fromLoading")
    el-row
      el-col(:span="12")
        span.el-descriptions__label 姓名：
        span.el-descriptions__content {{ editingObj?.name }}
      el-col(:span="12")
        span.el-descriptions__label 性别：
        span.el-descriptions__content {{ editingObj?.sex }}
      el-col(:span="12")
        span.el-descriptions__label 年龄：
        span.el-descriptions__content {{ editingObj?.age }}
      el-col(:span="12")
        span.el-descriptions__label 病人编号：
        span.el-descriptions__content 
          el-tooltip(:content="editingObj?.patientNumber",placement="top",trigger="hover") 
            span {{ editingObj?.patientNumber && editingObj?.patientNumber?.toString().length > 12 ? editingObj?.patientNumber.substring(0, 8) + "..." : editingObj?.patientNumber }}
      el-col(:span="12")
        span.el-descriptions__label 业务编号：
        span.el-descriptions__content
          el-tooltip(:content="editingObj?.inspectionNumber",placement="top",trigger="hover") 
            span {{ editingObj?.inspectionNumber && editingObj?.inspectionNumber?.toString().length > 12 ? editingObj?.inspectionNumber.substring(0, 8) + "..." : editingObj?.inspectionNumber }}

      el-col(:span="12")
        span.el-descriptions__label 检查设备：
        span.el-descriptions__content {{ editingObj?.deviceName }}
      el-col(:span="12")
        span.el-descriptions__label 申请医生：
        span.el-descriptions__content {{ editingObj?.applyDoctorName }}
      el-col(:span="12")
        span.el-descriptions__label 申请科室：
        span.el-descriptions__content {{ editingObj?.applyOfficeName }}
      el-col(:span="24")

        span.el-descriptions__label 申请项目：
        span.el-descriptions__content {{ editingObj?.projectName }}
      el-col(:span="24")
        span.el-descriptions__label 检查部位：
        span.el-descriptions__content {{ editingObj?.partName ?? "--" }}
      el-col(:span="24")
        el-row 
          .required-input *
          span.el-descriptions__label 影像所见：
        el-input(
          v-model="imageFindings"
          type="textarea"
          :rows="10"
          :autosize="false"
        )
      el-col(:span="24")
        el-row 
          .required-input *
          span.el-descriptions__label 诊断意见：
        el-input(
          v-model="diagnosticOpinion"
          type="textarea"
          :rows="10"
          :autosize="false"
        )
    .button-row 
      mt-button(type="primary",@click="saveBtnClick",v-loading="fromLoading") 保存结果
      .button-row-check
        span 是否上传危急值
        el-checkbox(v-model="criticalValueShow")
  criticalValue(v-model="criticalValueShow",:selectItem="editingItem")
</template>
<script setup lang="ts">
import type { RegistrationEntity } from "@scmt/api-pacs-registration";
import type { RegistrationConclusionEntity } from "@scmt/api-pacs-registration-conclusion";
import { useApi } from "@scmt/core-api";
import {
  useNotify
} from "@scmt/common-components";
import { useRequest } from "@scmt/common-hooks";
import criticalValue from "./critical-value.vue";
import { useUtils } from "@scmt/lib-utils";
const $utils = useUtils();
const $api = useApi();
const $notify = useNotify();
//添加 Props属性
const props = defineProps({
  selectItemTag: Object,
});
const fromLoading = ref(false);
const editingObj = ref<null | RegistrationEntity>(null);
const editingConclusioObj = ref<null | RegistrationConclusionEntity>(null);
const editingItem = ref<RegistrationConclusionEntity>($api.pacs.registrationConclusion.$$createEntity({}));

//诊断意见
const diagnosticOpinion = ref("");
//影像所见
const imageFindings = ref("");
//定义事件
const emit = defineEmits(['handleSave', 'valueChange']);

//是否显示上传危急值弹框
const criticalValueShow = ref(false);

const { request, data, loadingMap } = useRequest({
  //根据登记Id查询诊断意见
  registrationConclusion: async (registrationId: string) => {
    return await $api.pacs.registrationConclusion.queryRegistrationConclusionByRegistrationId(registrationId);
  },
  //保存诊断意见
  saveConclusion: (entity: RegistrationConclusionEntity) => {
    return $api.pacs.registrationConclusion.save(entity).then(() => {
      $notify.success("保存诊断意见成功");
      //save事件
      emit("handleSave");
    });
  },
  //更新登记单状态
  updateRegistrationState: (item: any) => {
    return $api.pacs.registration.updateRegistrationState(item).then(() => {
      $notify.success("更新登记单状态成功");
      // emit("handleQuery", {});
      // //保存成功关闭弹框
      // model.value = false;

    });
  }
});
const saveBtnClick = () => {
  fromLoading.value = true;
  if (!imageFindings.value) {
    $notify.error("请填写影像所见！");
    fromLoading.value = false;
    return;
  }
  if (!diagnosticOpinion.value) {
    $notify.error("请填写诊断意见！");
    fromLoading.value = false;
    return;
  }
  if (editingConclusioObj.value) {
    //myUUID = myUUID.replaceAll("-", "")
    editingConclusioObj.value.imageFindings = imageFindings.value;
    editingConclusioObj.value.diagnosticOpinion = diagnosticOpinion.value;
    editingConclusioObj.value.registrationId = editingObj.value?.id ?? "";
    editingConclusioObj.value.positive = 0;
    editingConclusioObj.value.auditDoctorId = editingObj.value?.id ?? "";
    editingConclusioObj.value.auditDoctorName = editingObj.value?.id ?? "";
    editingConclusioObj.value.auditTime = $utils.getCurrentTimeString();

  }
  //@ts-ignore
  request("saveConclusion", editingConclusioObj.value).then(() => {
    let registrationState = editingObj.value?.registrationState ?? "0"
    if (registrationState <= "6") {
      registrationState = "6"
    }
    request("updateRegistrationState", { registrationState: registrationState, ids: editingObj.value?.id ?? "" });
  });

}
watch(
  () => props.selectItemTag,
  (item) => {
    criticalValueShow.value = false;
    editingObj.value = $api.pacs.registration.$$createEntity({});
    if (item && item.id) {
      item.patientType = item.patientType + "";
      editingObj.value = $api.pacs.registration.$$createEntity(item);
      request("registrationConclusion", item.id).then(() => {
        editingConclusioObj.value = $api.pacs.registrationConclusion.$$createEntity(data.registrationConclusion ?? {});
        imageFindings.value = editingConclusioObj.value.imageFindings;
        diagnosticOpinion.value = editingConclusioObj.value.diagnosticOpinion;
      });
    }
  },
  {
    immediate: true,
  }
);
watch(
  () => loadingMap.saveConclusion,
  (item) => {
    fromLoading.value = item ?? false;
  },
  {
    immediate: true,
  }
);
watch(
  () => imageFindings.value,
  () => {
    if (editingConclusioObj.value) {
      editingConclusioObj.value.imageFindings = imageFindings.value;
      editingConclusioObj.value.diagnosticOpinion = diagnosticOpinion.value;
    }
    //结论改变事件
    emit("valueChange", { imageFindings: imageFindings.value, diagnosticOpinion: diagnosticOpinion.value });
    editingItem.value = $api.pacs.registrationConclusion.$$createEntity(editingConclusioObj.value ?? {});
  },
  {
    immediate: true,
  }
);
watch(
  () => diagnosticOpinion.value,
  () => {
    if (editingConclusioObj.value) {
      editingConclusioObj.value.imageFindings = imageFindings.value;
      editingConclusioObj.value.diagnosticOpinion = diagnosticOpinion.value;
    }
    //结论改变事件
    emit("valueChange", { imageFindings: imageFindings.value, diagnosticOpinion: diagnosticOpinion.value });
    editingItem.value = $api.pacs.registrationConclusion.$$createEntity(editingConclusioObj.value ?? {});
  },
  {
    immediate: true,
  }
);
</script>

<style lang="less" scoped>
.pacs-conclusion-form {
  font-size: 15px;

  .el-descriptions__label {
    display: inline-block;
    text-align: right;
    width: 80px;
    font-weight: 600;
    margin-right: var(--mt-margin);
  }

  .el-descriptions__content {
    color: var(--el-text-color-regular);
  }

  .required-input {
    color: red;
    font-weight: bold;
    margin-left: 2px;
  }

  .el-col {
    padding: 5px;

  }

  .button-row {
    display: flex;

    &-check {
      margin-left: var(--mt-margin);

      .el-checkbox {
        margin-left: var(--mt-margin);
      }
    }
  }
}
</style>